package ift605.EquationWithTrainingAgents.behavior.reception;

import java.util.List;

import javax.naming.directory.InvalidAttributesException;

import udes.ds.agent.AbstractEquation;

import ift605.EquationWithAgent.server.EquationMessage;
import ift605.EquationWithTrainingAgents.behavior.common.ReceiveDerivativeBehavior;
import ift605.EquationWithTrainingAgents.struct.DerivationInfo;

public class ReplaceDerivationPartBehavior extends ReceiveDerivativeBehavior{
	private static final long serialVersionUID = 1L;

	@Override
	@SuppressWarnings("unchecked")
	public boolean applyDerivative(EquationMessage aEquationMessage, DerivationInfo aDerivationInfo) {
		try {
			Object[] values = aDerivationInfo.Mess.derived.getValues();
			List<Integer> index = (List<Integer>)aDerivationInfo.Variation;
			Object currentElement;
			
			while(index.size() > 0){
				currentElement = values[index.get(0)];
				values = ((AbstractEquation)currentElement).getValues();
				if(index.size() > 1){
					values = ((AbstractEquation)currentElement).getValues();
				}
				else{
					currentElement = aEquationMessage.getEquation();
				}
				index.remove(0);
			}
			
			aDerivationInfo.Mess.derived.setValues(values);
			return true;
		} 
		catch (InvalidAttributesException e) {
			e.printStackTrace();
		}
		
		return false;
	}
}
